package blancosMikel.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import blancosMikel.controller.beans.marcaBeans;

public class marcaDAO extends SigaabmDAO {
	/**
	 * ESTE METODO CARGARA las marcas
	 * 
	 * @author Manuel
	 * @throws Exception
	 * @since 7 de julio de 2013
	 * @category Modulos informativos
	 */
	public ArrayList<marcaBeans> selectMarcas() throws Exception {
		StringBuilder query = new StringBuilder();
		ArrayList<marcaBeans> promoList = new ArrayList<marcaBeans>();
		try {
			query.append("Select nombre from marca");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.Marca.selectMarcas.Ejecutado----------> "
					+ query.toString());

			marcaBeans bean = null;
			while (rs.next()) {
				bean = new marcaBeans();
				bean.setNombre(rs.getString(1));
				promoList.add(bean);
			}
			System.out.println("dao.Marca.selectMarcas----------> Cargado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return promoList;
	}
	
	public ArrayList<Object> MarcasNumCatalogos() throws Exception {
		StringBuilder query = new StringBuilder();
		ArrayList<Object> lista = new ArrayList<Object>();
		try {
			query.append("SELECT ")
			.append("id_marca AS id, ")
			.append("nombre, ")
			.append("url, ")
			.append("(SELECT COUNT(id_catalogos) FROM catalogos WHERE id_marca = id) AS numeroCatalogos ")
			.append(" FROM marca");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.Marca.selectMarcas.Ejecutado----------> "
					+ query.toString());
			Object[] bean = new Object[4];
			while (rs.next()) {
				bean[0] = rs.getInt(0);// id
				bean[1] = rs.getString(1);//nombre
				bean[2] = rs.getString(2);//url
				bean[3] = rs.getInt(3);//cuenta
				lista.add(bean);
			}
			System.out.println("dao.Marca.selectMarcas----------> Cargado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return lista;
	}
	
	
}
